package com.ny.view;

import com.ny.entity.Student;
import com.ny.service.ManagerService;
import com.ny.service.OrderService;
import com.ny.service.ProductService;
import com.ny.service.StudentService;
import com.ny.service.impl.ManagerServiceImpl;
import com.ny.service.impl.OrderServiceImpl;
import com.ny.service.impl.ProductServiceImpl;
import com.ny.service.impl.StudentServiceImpl;

import javax.swing.*;
import java.util.List;
import java.util.Scanner;

public class ManagerView {
    private static ManagerService managerService=new ManagerServiceImpl();// 创建业务逻辑层对象
    private static StudentService studentService=new StudentServiceImpl();
    private static ProductService productService=new ProductServiceImpl();
    private static OrderService orderService=new OrderServiceImpl();
    private static Scanner scanner = new Scanner(System.in);// 创建扫描器对象
    public static void main(String[] args) throws Exception {// 登录
        System.out.println( "管理员登录：");
        System.out.println("请输入用户名：");
        String name = scanner.next();
        System.out.println("请输入密码：");
        String password = scanner.next();

        if (managerService.login(name, password) != null) {// 登录成功
            boolean running = true;// 控制循环
            while (running) {  // 循环控制
                displayMenu();// 显示菜单
                int choice = Integer.parseInt(scanner.next());// 获取用户输入
                switch (choice) {// 选择
                    case 1:
                        managerStudents();// 处理用户管理
                        break;

                    case 2:
                        managerProducts();// 处理商品管理
                        break;

                    case 3:
                        managerOrders();// 处理订单管理
                        break;

                    case 4:
                        System.out.println("退出系统");// 退出系统
                        running = false;// 退出循环
                        break;// 结束switch
                        default:
                            System.out.println("无效的选择，请重新输入。");// 无效的选择
                }
            }
        }else {
            System.out.println("登录失败，用户名或密码错误");// 登录失败
        }
    }
    private static void displayMenu() {
        System.out.println("欢迎管理员登录");
        System.out.println("\n=====菜单=====");
        System.out.println("1.用户管理");
        System.out.println("2.商品管理");
        System.out.println("3.订单管理");
        System.out.println("4.退出系统");
        System.out.println("请选择(1-4)：");// 提示用户输入
    }
    private static void managerStudents(){
        System.out.println("\n用户管理:");
        System.out.println("1.查看所有用户");
        System.out.println("2.添加用户");
        System.out.println("3.修改用户信息");
        System.out.println("4.删除用户");
        System.out.println("5.请选择(1-4)：");// 提示用户输入
        int choice = Integer.parseInt(scanner.next());

        try {
            switch (choice) {
                case 1:
                    viewAllStudents();
                    break;
                case 2:
                    addStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    deleteStudent();
                    break;
                default:
                    System.out.println("无效的选择，请重新输入");
            }
        }catch (Exception e) {
            System.out.println("操作失败：" + e.getMessage());
            e.printStackTrace();
        }
    }


    private static void viewAllStudents() throws Exception {
        List<Student> students = studentService.findAllStudents();
        System.out.println("\n---所有用户列表---：");
        for (Student student : students) {
            System.out.println("ID" + student.getStudentID() +
                    ", 姓名" + student.getName() +
                    ", 手机" + student.getPhone());
        }
    }

    private static void addStudent(){
        System.out.println("请输入用户姓名：");
        int studentID = scanner.nextInt();
        System.out.println("请输入用户密码：");
        String name = scanner.next();
        System.out.println("请输入用户手机号：");
        student.setPhone(phone);

    }

}
